<?php

namespace app\admin\model\coupons;

use think\Model;


class Coupons extends Model
{

    

    

    // 表名
    protected $name = 'coupons';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'begintime_text',
        'endtime_text',
        'statusdata_text',
        'platform_text'
    ];
    

    public function couponcategory()
    {
        return $this->belongsTo('app\admin\model\coupons\Category', 'coupon_category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getStatusdataList()
    {
        return ['0' => __('Statusdata 0'), '1' => __('Statusdata 1'), '2' => __('Statusdata 2'), '3' => __('Statusdata 3'),'4' => __('Statusdata 4')];
    }

    public function getPlatformList()
    {
        return ['1' => __('Platform 1'), '2' => __('Platform 2')];
    }


    public function getBegintimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['begintime']) ? $data['begintime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }
     public function getAppointmenttimeTextAttr($value, $data)
        {
            $value = $value ? $value : (isset($data['appointmenttime']) ? $data['appointmenttime'] : '');
            return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
        }


    public function getFinishtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['finishtime']) ? $data['finishtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getStatusdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['statusdata']) ? $data['statusdata'] : '');
        $list = $this->getStatusdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPlatformTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['platform']) ? $data['platform'] : '');
        $list = $this->getPlatformList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setBegintimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

     protected function setAppointmenttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setFinishtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}